<template>
  <div class="app-container">
    <a-title @clickPreview="preview"></a-title>
    <el-form :model="queryForm" ref="queryForm" :inline="true" label-width="110px">
      <el-form-item label="部门简称" prop="deptShortName">
        <el-input
          v-model="queryForm.deptShortName"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部级审批人" prop="leader">
        <!-- <el-select v-model="queryForm.leader" placeholder="请选择" size="small">
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
            <span style="float: left">{{ item.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
          </el-option>
        </el-select>-->
        <el-input
          v-model="queryForm.leader"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所级审批人" prop="whatBusinessLeader">
        <el-input
          v-model="queryForm.whatBusinessLeader"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
        <!-- <el-select
          v-model="queryForm.whatBusinessLeader"
          placeholder="请选择"
          style="width:100%;"
          :filterable="true"
          size="small"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
            <span style="float: left">{{ item.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
          </el-option>
        </el-select>-->
      </el-form-item>

      <el-form-item label="业务部门负责人" prop="techLeader">
        <el-input
          v-model="queryForm.techLeader"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
        <!-- <el-select
          v-model="queryForm.techLeader"
          placeholder="请选择"
          style="width:100%;"
          :filterable="true"
          size="small"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
            <span style="float: left">{{ item.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
          </el-option>
        </el-select>-->
      </el-form-item>
      <el-form-item label="业务所负责人" prop="whatBusinessTechLeader">
        <el-input
          v-model="queryForm.whatBusinessTechLeader"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
        <!-- <el-select
          v-model="queryForm.whatBusinessTechLeader"
          placeholder="请选择"
          style="width:100%;"
          :filterable="true"
          size="small"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
            <span style="float: left">{{ item.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
          </el-option>
        </el-select>-->
      </el-form-item>
      <el-form-item label="所属业务所" prop="whatBusiness">
        <el-select
          v-model="queryForm.whatBusiness"
          placeholder="请选择"
          style="width:100%;"
          :filterable="true"
          size="small"
        >
          <el-option
            v-for="item in whatBusiness"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div style="margin-left:20px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dept:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dept:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-upload2" size="mini">导入</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleDownload"
          v-hasPermi="['system:dept:export']"
        >导出</el-button>
      </el-col>
    </el-row>
    <el-table
      stripe
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
      @sort-change="tableSortChange"
      :default-sort="{prop: 'orderNum', order: 'ascending'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column
        label="业务部门名称"
        width="200"
        align="left"
        prop="deptName"
        :show-overflow-tooltip="true"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />-->
      <el-table-column
        label="部门简称"
        width="200"
        align="left"
        prop="deptShortName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column label="部级审批人" align="center" prop="leadersName" />
      <el-table-column label="所级审批人" align="center" prop="whatBusinessLeadersName" />
      <el-table-column label="业务部门负责人" align="center" prop="techLeadersName" />
      <el-table-column label="业务所负责人" align="center" prop="whatBusinessTechLeadersName" />
      <el-table-column
        label="所属业务所"
        align="center"
        prop="whatBusinessName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <!-- <el-table-column label="业务分管领导" align="center" prop="deputyDirsName" /> -->
      <!-- <el-table-column label="公章管理员" align="center" prop="cachetManagesName" /> -->
      <el-table-column
        label="排序编号"
        align="center"
        prop="orderNum"
        width="100px"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <!-- <el-table-column label="报表分组" align="center" prop="reportGroup" />
      <el-table-column label="报表名称" align="center" prop="reportName" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="100px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dept:edit']"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :visible.sync="open" width="1000px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="部门简称" prop="deptShortName">
                <el-input v-model="form.deptShortName" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="业务部门名称" prop="deptName">
                <el-input v-model="form.deptName" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="所属业务所" prop="whatBusiness">
                <el-select v-model="form.whatBusiness" placeholder="请选择" style="width:100%">
                  <el-option
                    v-for="item in whatBusiness"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content">
              <el-form-item label="排序编号" prop="orderNum">
                <el-input-number
                  v-model="form.orderNum"
                  placeholder="请输入"
                  :precision="2"
                  :step="0.1"
                  style="width:100%"
                  :min="0"
                />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="业务分管领导" prop="deputyDirs">
                <el-select
                  v-model="form.deputyDirs"
                  placeholder="请选择"
                  style="width:100%;"
                  :filterable="true"
                  :multiple="true"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.nickName"
                    :value="item.userId"
                  >
                    <span style="float: left">{{ item.nickName }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content">
              <el-form-item label="匹配行政组织架构" prop="standardOrgId">
                <el-input v-model="form.standardOrgId" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <!-- <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="公章管理员" prop="cachetManages">
                <el-select
                  v-model="form.cachetManages"
                  placeholder="请选择"
                  style="width:100%;"
                  :filterable="true"
                  :multiple="true"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.nickName"
                    :value="item.userId"
                  >
                    <span style="float: left">{{ item.nickName }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部级审批人" prop="isvalid">
              <el-select
                v-model="form.leaders"
                placeholder="请选择"
                style="width:100%;"
                :filterable="true"
                :multiple="true"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                  <span style="float: left">{{ item.nickName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="grid-content">
              <el-form-item label="所级审批人" prop="whatBusinessLeaders">
                <el-select
                  v-model="form.whatBusinessLeaders"
                  placeholder="请选择"
                  style="width:100%;"
                  :filterable="true"
                  :multiple="true"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.nickName"
                    :value="item.userId"
                  >
                    <span style="float: left">{{ item.nickName }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务部门负责人" prop="techLeaders">
              <el-select
                v-model="form.techLeaders"
                placeholder="请选择"
                style="width:100%;"
                :filterable="true"
                :multiple="true"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                  <span style="float: left">{{ item.nickName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务所负责人" prop="whatBusinessTechLeaders">
              <el-select
                v-model="form.whatBusinessTechLeaders"
                placeholder="请选择"
                style="width:100%;"
                :filterable="true"
                :multiple="true"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                  <span style="float: left">{{ item.nickName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报表分组" prop="reportGroup">
              <el-input v-model="form.reportGroup" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报表名称" prop="reportName">
              <el-input v-model="form.reportName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
// import { systemProjectList, getProject, updateProject } from "@/api/system/systemProject";
import { userAllList } from "@/api/system/user";
import { getNewDept, getNewDeptInfo, deleteNewDept, addNewDept, updateNewDept, exportNewDept } from "@/api/system/organization"
import aTitle from "@/components/Title";

export default {
  name: "Organization",
  components: {
    aTitle
  },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      guid: undefined,
      multiple: true,
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        deptShortName: undefined,
        orderNum: undefined,
        whatBusiness: undefined,
        leader: undefined,
        techLeader: undefined,
        deputyDir: undefined,
        cachetManage: undefined
      },
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 任务模板表格数据
      dataList: [],
      ids: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验

      rules: {
        deptShortName: [
          { required: true, message: "部门简称不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "排列顺序不能为空", trigger: "blur" }
        ]
      },
      userList: [],
      //部级审批人集合
      leaderUserList: [],
      //业务部门负责人集合
      techLeaderUserList: [],
      //所级审批人集合
      whatBusinessLeadersUserList: [],
      //业务所负责人集合
      whatBusinessTechLeadersUserList: [],
      whatBusiness: []
    };
  },
  created () {
    this.getList();

    // userAllList({ userRoles: "部级审批人" }).then(response => {
    //   this.leaderUserList = response.data;
    // });
    // userAllList({ userRoles: "业务部门负责人" }).then(response => {
    //   this.techLeaderUserList = response.data;
    // });
    // userAllList({ userRoles: "所级审批人" }).then(response => {
    //   this.whatBusinessLeadersUserList = response.data;
    // });
    // userAllList({ userRoles: "业务所负责人" }).then(response => {
    //   this.whatBusinessTechLeadersUserList = response.data;
    // });

    userAllList().then(response => {
      this.userList = response.data;
    });
    this.getDicts("tjec_what_business").then(response => {
      this.whatBusiness = response.data;
    });
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}系统管理/组织管理.xlsx`
      )}&name=${encodeURIComponent("组织管理.xlsx")}`;
    },
    handleAdd () {
      this.form = {}
      this.guid = undefined;
      this.open = true;
    },
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.guid)
      this.multiple = !selection.length;
    },
    tableSortChange (column) {
      this.queryForm.pageNum = 1;
      if (column.order === "descending") {
        this.queryForm.sortBy = column.prop;
        this.queryForm.order = "desc";
      } else {
        this.queryForm.sortBy = column.prop;
        this.queryForm.order = "asc";
      }
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.open = true;
      this.guid = row.guid;
      getNewDeptInfo(this.guid).then(res => {
        if (res.code == 200) {
          this.form = res.data;
        } else {
          this.msgError(res.msg);
        }
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.guid === undefined) {
            addNewDept(this.form).then(res => {
              if (res.code === 200) {
                this.getList()
                this.open = false;
                this.msgSuccess(res.msg)
              }
            })
          } else {
            updateNewDept(this.form).then(res => {
              this.getList()
              this.open = false;
              this.msgSuccess(res.msg)
            })
          }
        }
      });
    },
    /** 查询任务列表 */
    getList () {
      this.loading = true;
      getNewDept(this.queryForm).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryForm.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        guid: undefined
      };
      this.resetForm("form");
    },
    handleDelete () {
      this.$confirm('是否确认删除?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return deleteNewDept(this.ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });

    },
    handleDownload () {
      exportNewDept(this.queryForm).then(res => {
        if (res.code === 200) {
          this.download(res.msg)
        }
      })
    }
  }
};
</script>
<style lang="scss" scoped>
</style>